//
//  TNMatrixViewController.m
//  WebPDA
//
//  Created by Justin Yip on 8/12/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "TNMatrixViewController.h"
#import "TNMatrixViewCell.h"
#import <QuartzCore/QuartzCore.h>


@implementation TNMatrixViewController
@synthesize menuBundle;
@synthesize menuItems;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	NSLog(@"The index of the alert button clicked is %d", buttonIndex);
}

-(void)didSelectMenuItem:(id)sender {
	//type check
	UIButton *button = (UIButton*)sender;
	TNMatrixViewCell *cell = (TNMatrixViewCell*)button.superview;
	NSLog(@"selected index:%d",cell.index);

	UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"选择了" 
														 message:[NSString stringWithFormat:@"%d", cell.index] 
														delegate:self 
											   cancelButtonTitle:@"OK" 
											   otherButtonTitles:nil] autorelease];
	[alertView show];
	
	/*
	 handle Action
	 */
	NSDictionary *item = [menuItems objectAtIndex:cell.index];
	NSString *itemAction = [item objectForKey:@"Action"];
	if (itemAction && [self respondsToSelector:NSSelectorFromString(itemAction)]) {
		[self performSelector:NSSelectorFromString(itemAction)];
	}
}

-(void)loadBundle:(NSString*)bundleName {
	[self loadFile:@"Root.plist" inBundle:bundleName];
}

-(void)loadFile:(NSString*)fileName inBundle:(NSString*)bundleName {
	
	NSLog(@"Matrix View load file %@ in bundle %@", fileName, bundleName);
	
	NSString *menuBundlePath = [[NSBundle mainBundle] pathForResource:bundleName ofType:@"bundle"];
	menuBundle = [[NSBundle alloc] initWithPath:menuBundlePath];
	NSString *rootPath = [menuBundlePath stringByAppendingPathComponent:fileName];
	NSDictionary *rootDict = [[NSDictionary alloc] initWithContentsOfFile:rootPath];
	self.menuItems = [rootDict objectForKey:@"Menu"];
	
	for (int i = 0; i < [menuItems count] && i < 16; i++) {
		
		// load one cell
		NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TNMatrixViewCell" owner:nil options:nil];
		NSAssert([nib count] == 1, @"view inside TNMatrixViewCell.xib != 1");
		TNMatrixViewCell *cell = [nib objectAtIndex:0];
		
		// calculate the frame
		CGRect frame = cell.frame;
		frame.origin.x = i % 4 * 80;
		frame.origin.y = i / 4 *100;
		cell.frame = frame;
		
		// i18n title
		NSDictionary *menuDict = [menuItems objectAtIndex:i];
		NSString *title= [menuDict objectForKey:@"Title"];
		cell.label.text = NSLocalizedStringFromTableInBundle(title, @"Root", menuBundle, @"");
		
		//set image
		NSString *imageName = [menuDict objectForKey:@"Image"];
		if (nil != imageName) {
			[cell.button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
		}
		
		//handle touch event
		cell.index = i;//remember your position
		[cell.button addTarget:self action:@selector(didSelectMenuItem:) forControlEvents:UIControlEventTouchUpInside];
		
		[self.view addSubview:cell];
	}
	
	NSString *bgImageName = [rootDict objectForKey:@"BackgroundImage"];
	if (bgImageName) {
		self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:bgImageName]];
		
	}
	
	//NSString *bgColor = [rootDict objectForKey:@"BackgroundColor"];
	if (YES) {
		self.view.backgroundColor = [UIColor grayColor];
	}
	
	
	[rootDict release];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[menuBundle release];
	[menuItems release];
    [super dealloc];
}


@end
